package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.Money;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j  //日志
@RestController
@RequestMapping("/stu")
public class StudentController {
    @PostMapping("/add")
    public Student addNewDrink(@RequestBody Student student){
        log.info("新增: {}",student);
        // 后期使用数据库insert
        LocalDate localDate = LocalDate.of(2002, 02, 14);
        Student addStudent= Student.builder().id(1L)
                .major("移动应用开发")
                .name("张三")
                .adminssionDate(localDate)
                .build();
        return addStudent;

        //two
//        return Student.builder()
//                .id(1L)
//                .adminssjonDate(LocalDate.parse("2002-02-14"))
//                .name("明明")
//                .major("移动应用开发")
//                .build();
    }


    @DeleteMapping("/del/{id}")
    public String delete(@PathVariable Integer id){
        log.info("要删除的id: {}",id);
        //后期使用数据库
        return "已删除";
    }


    @GetMapping("/all")
    public List<Student> getall(){
        LocalDate localDate = LocalDate.of(2002, 02, 14);
        List<Student> results=new ArrayList<>();
        results.add(Student.builder()
                .id(1L)
                .major("移动应用开发")
                .name("张三")
                .adminssionDate(localDate)
                .build());
        results.add(Student.builder()
                .id(2L)
                .major("移动应用开发")
                .name("张三")
                .adminssionDate(localDate)
                .build());
        return results;
    }


    @GetMapping("/{id}")
    public Student get(@PathVariable Integer id){
        LocalDate localDate = LocalDate.of(2002, 02, 14);
        log.info("查询的id为: {}",id);
        Student student=(Student.builder()
                .id(1L)
                .major("移动应用开发")
                .name("张三")
                .adminssionDate(localDate)
                .build());
        return student;
    }


    @PostMapping("/update")
    public Student put(){
        LocalDate localDate = LocalDate.of(2002, 02, 14);
        // 后期使用数据库update
        Student add= Student.builder().id(4L)
                .major("移动应用开发")
                .name("张三")
                .adminssionDate(localDate)
                .build();
        return add;
    }



    @PostMapping(value = "/batch",consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})
    public List batchAddNewDrink(MultipartFile file){
        List<Student> list=new ArrayList<>();
        try {
            InputStreamReader inputStreamReader=new InputStreamReader(file.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line="";
            while ((line=bufferedReader.readLine())!=null){
                String [] args=line.split(",");
                String date1 = args[3];
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate date2 = LocalDate.parse(date1, fmt);
                Student student= Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .major(args[2])
                        .adminssionDate(date2).build();
                list.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
